package com.cyt.wx.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.BaseUtil;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.wx.dao.WxMemberDao;
import com.cyt.wx.model.po.WxMember;
import com.cyt.wx.model.vo.WxMemberVo;
import com.cyt.wx.service.WxMemberService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 微信端-会员管理Service层实现类
 *
 * @author caideyong
 * @date 2019-7-9 8:37:55
 */
@Service("wxMemberService")
public class WxMemberServiceImpl extends BaseServiceImpl<WxMember> implements WxMemberService {

    @Resource
    private WxMemberDao wxMemberDao;

    @Autowired
    public void setBaseDao(WxMemberDao wxMemberDao) {
        this.baseDao = wxMemberDao;
    }

    /**
     * 查询对象集合
     *
     * @param wxMemberVo 对象
     * @return List 对象集合
     * @author caideyong
     * @date 2019-7-9 8:37:55
     */
    @Override
    public List<WxMember> listExample(WxMemberVo wxMemberVo) {
        List<WxMember> list=wxMemberDao.selectByWxMemberVo(wxMemberVo);
        if(list!=null&&list.size()>0){
            for(WxMember wxMember:list){
                if(StringUtils.isNotEmpty(wxMember.getNickName())){
                    wxMember.setNickName(BaseUtil.decodeBase64(wxMember.getNickName()));
                }
            }
        }
        return list;
    }
    /**
     * 通过openId获取会员信息
     *
     * @return List 对象集合
     * @author caideyong
     * @date 2019-7-9 8:37:55
     */
    @Override
    public WxMember selectMemberByOpenId(String openid) {
        return wxMemberDao.selectMemberByOpenId(openid);
    }

    @Override
    public WxMember selectMemberByToken(String token) {
        WxMember wxMember=wxMemberDao.selectMemberByToken(token);
        if(StringUtils.isNotEmpty(wxMember.getNickName())){
            wxMember.setNickName(BaseUtil.decodeBase64(wxMember.getNickName()));
        }
        return wxMember;
    }

    /**
     * 分页查询
     *
     * @param wxMemberVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author caideyong
     * @date 2019-7-9 8:37:55
     */
    @Override
    public ReturnResult page(WxMemberVo wxMemberVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        if(wxMemberVo!=null&&StrUtil.isNotEmpty(wxMemberVo.getNickName())){
            wxMemberVo.setNickName(BaseUtil.encodeBase64String(wxMemberVo.getNickName()));
        }
        List<WxMember> list = listExample(wxMemberVo);
        PageInfo<WxMember> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 分页查询
     *
     * @param wxMemberVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author caideyong
     * @date 2019-7-9 8:37:55
     */
    @Override
    public ReturnResult pageUser(WxMemberVo wxMemberVo, int pageNum, int pageSize,SysUser sysUser) {
        PageHelper.startPage(pageNum, pageSize);
        wxMemberVo.setUserId(sysUser.getId());
        if(wxMemberVo!=null&&StrUtil.isNotEmpty(wxMemberVo.getNickName())){
            wxMemberVo.setNickName(BaseUtil.encodeBase64String(wxMemberVo.getNickName()));
        }
        List<WxMember> list = listExample(wxMemberVo);
        PageInfo<WxMember> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param wxMemberVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author caideyong
     * @date 2019-7-9 8:37:55
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(WxMemberVo wxMemberVo, SysUser sysUser) {
        if (StrUtil.isEmpty(wxMemberVo.getId())) {
            WxMember wxMember = new WxMember(sysUser);
            wxMember.setOpenid(wxMemberVo.getOpenid());
            wxMember.setHeadLogo(wxMemberVo.getHeadLogo());
            wxMember.setNickName(wxMemberVo.getNickName());
            wxMember.setToken(BaseUtil.uId());
           /* SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date AccessTokenEndTime=simpleDateFormat.parse(wxMemberVo.getTokenEndTime());
                wxMember.setTokenEndTime(AccessTokenEndTime);
            } catch(ParseException px) {
                px.printStackTrace();
            }*/
            wxMember.setTokenEndTime(new Date());//暂时默认为当前时间
            wxMember.setApplyId(wxMemberVo.getApplyId());
            int num = wxMemberDao.insert(wxMember);
            if (num > 0) {
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            WxMember wxMember = wxMemberDao.selectByPrimaryKey(wxMemberVo.getId());
            wxMember.setUpdateTime(new Date());
            wxMember.setUpdateUser(sysUser.getId());
            int num = wxMemberDao.updateByPrimaryKey(wxMember);
            if (num > 0) {
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param wxMemberVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author caideyong
     * @date 2019-7-9 8:37:55
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(WxMemberVo wxMemberVo, SysUser sysUser) {
        //要修改的字段
        WxMember wxMember = new WxMember();
        wxMember.setRemoveTag(BaseConstant.REMOVE_TAG_YES);
        wxMember.setUpdateUser(sysUser.getId());
        wxMember.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(WxMember.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wxMemberVo.getId())) {
            criteria.andIn("id", Arrays.asList(wxMemberVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        wxMemberDao.updateByExampleSelective(wxMember, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param wxMemberVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author caideyong
     * @date 2019-7-9 8:37:55
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(WxMemberVo wxMemberVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(WxMember.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wxMemberVo.getId())) {
            criteria.andIn("id", Arrays.asList(wxMemberVo.getId().split(",")));
        }
        wxMemberDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }
}